/**
 * 
 */
package edu.cornell.cs.lsi.projone.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import edu.cornell.cs.lsi.projone.common.LSIConstants;
import edu.cornell.cs.lsi.projone.common.LSIException;

/**
 * @author poornima
 * 
 */
public class PropertyLoaderUtil {

	private final static Log LOGGER = LogFactoryImpl
			.getLog(PropertyLoaderUtil.class);

	private static Properties prop;

	public PropertyLoaderUtil() throws LSIException {
		init();
	}

	public static String getValue(String key) throws LSIException {
		if (key == null || StringUtils.isEmpty(key)) {
			LOGGER.error("ProperLoader getValue(): Incoming parameter is empty.");
			throw new LSIException("Incoming parameter is empty");
		}
		if (prop == null) {
			init();
		}
		return prop.getProperty(key);
	}

	private static void init() throws LSIException {
		File file = new File("hehehe");
		try {
			file.createNewFile();
		} catch (IOException exp) {

		}
		prop = new Properties();
		try {
			prop.load(new FileInputStream(LSIConstants.CONFIG_FILE));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new LSIException(e, "File not Found error");
		} catch (IOException e) {
			e.printStackTrace();
			throw new LSIException(e, "I/O error while reading the file");
		}
	}
}